package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.vo.AnnouncementVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.AnnouncementsApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class AnnouncementsService {
    @Reference
    private AnnouncementsApi announcementsApi;

    public ResponseEntity<Object> queryAnnouncements(Integer page, Integer pagesize) {
       IPage<Announcement> iPage=announcementsApi.queryAnnouncements(page,pagesize);
        List<AnnouncementVo> voList=new ArrayList<>();
       if (iPage.getRecords()!=null&&iPage.getRecords().size()>0){
           for (Announcement announcement : iPage.getRecords()) {
               AnnouncementVo announcementVo = new AnnouncementVo();
               announcementVo.setId(announcement.getId().toString());
               announcementVo.setTitle(announcement.getTitle());
               announcementVo.setDescription(announcement.getDescription());
               announcementVo.setCreatedDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(announcement.getCreated()));
                voList.add(announcementVo);
           }
       }



       //这里要卡住
       PageResult pageResult=new PageResult(page,pagesize,(int)iPage.getTotal(),voList);
        return ResponseEntity.ok(pageResult);
    }
}
